package com.server.controller;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.server.common.base.PathManager;
import com.server.common.base.ProjectInfor;
import com.server.common.base.SessionData;
import com.server.service.ICommonDataService;
import com.server.service.ILoginService;
import com.server.service.IUserManagementService;
import com.server.web.JSONSerielizer;
import com.server.web.OutputUtil;
import com.server.web.SessionUtil;

@Controller
@RequestMapping("user/infor")
public class UserInforController {

	private static final String TABLE_NAME = "reward.user";
	private static final String TABLE_NAME_EMPLOY = "reward.employ";
	private static final String VIEW_NAME = "reward.v_user_employ";
	
	@Autowired
	private ICommonDataService commonDataService;
	@Autowired
	private IUserManagementService userManagementService;
	@Autowired
	private ILoginService loginService;
	
	 /**
	 * 个人资料查看
	 * @param request
	 * @param response
	 * @return
	 * @author ServerZhang
	 * @date 2017年3月13日
	 */
	
	@RequestMapping("")
	public ModelAndView infor(HttpServletRequest request,
			HttpServletResponse response) {
		String cardId = SessionUtil.getStringAttri(request, SessionData.cardIdKey);
		Map<String, Object> formData = commonDataService.getFormData(VIEW_NAME, "CardID", cardId, "State=1", null);
		ModelAndView mv = new ModelAndView("userCenter/infor/userForm");
		mv.addObject("rootPath",ProjectInfor.ROOT_PATH);
		mv.addObject("action", "check");
		mv.addObject("business", "user");
		mv.addObject("titleName", "个人信息");
		mv.addObject("id", formData.get("EmployId"));
		mv.addObject("cardId", cardId);
		
		mv.addObject("configStr", userManagementService.getConfigStr());
		
		StringBuffer serverBuffer = new StringBuffer();
		String serverName = request.getServerName();
		int serverPort = request.getServerPort();
		serverBuffer.append("http://").append(serverName)
		.append(":").append(serverPort).append("/")
		.append(PathManager.FILE_MAPPING);
		mv.addObject("fileServer",serverBuffer.toString());
		return mv;
	}
	
	@RequestMapping("psw")
	public ModelAndView psw(HttpServletRequest request,
			HttpServletResponse response) {
		String cardId = SessionUtil.getStringAttri(request, SessionData.cardIdKey);
		ModelAndView mv = new ModelAndView("userCenter/infor/pswForm");
		mv.addObject("rootPath",ProjectInfor.ROOT_PATH);
		return mv;
	}
	@RequestMapping("save")
	public void save(HttpServletRequest request,
			HttpServletResponse response){
		String cardId = SessionUtil.getStringAttri(request, SessionData.cardIdKey);
		String oldPsw = request.getParameter("oldPsw");
		String newPsw = request.getParameter("newPsw");
		
		Map<String, Object> map = new HashMap<String, Object>();
		Boolean isSuccess = true;
		String info = null;
		
		isSuccess = loginService.check(cardId, oldPsw);
		if (!isSuccess) {
			map.put("isSuccess", isSuccess);
			info = "原密码输入不正确！";
			map.put("info", info);
		}else {
			isSuccess = loginService.changePsw(cardId, newPsw);
			map.put("isSuccess", isSuccess);
			info = isSuccess ? "密码修改成功。" : "密码修改失败！";
			map.put("info", info);
		}
		OutputUtil.returnString(response, JSONSerielizer.toJson(map, null, null));
	}
	
}
